package org.vt.ece3574.pictoslider.menus;

import org.vt.ece3574.pictoslider.HighScoreManager;
import org.vt.ece3574.pictoslider.HighScoreManager.Difficulty;
import org.vt.ece3574.pictoslider.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Spinner;
import android.widget.TextView;

/**
 * This class represents the Settings Menu activity, where the user can choose
 * from various themes and game options. Right now this screen does not do
 * anything.
 * 
 * @author Michael Drescher
 */
public class SettingsMenu extends Activity {

	// Delete Warning Dialog Parameters
	private static final int DELETE_WARNING_HORIZONTAL_PADDING = 5;
	private static final int DELETE_WARNING_VERTICAL_PADDING = 10;
	private static final int DELETE_WARNING_TEXT_SIZE = 20;
	private static final String DELETE_WARNING_TEXT = "Delete all of the high scores?";

	/**
	 * String key used for Shared Preferences
	 */
	public static final String SETTINGS_PREFS = "Settings Preferences";

	/**
	 * String key used for Shared Preferences for integer theme selected.
	 */
	public static final String THEME = "Theme";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Themes.onActivityCreateSetTheme(this);
		Themes.onActivityCreateSetContentView(this, R.layout.settings_menu);

		// Set the text color of both TextViews in this Activity
		((TextView) findViewById(R.id.textView2)).setTextColor(Themes
				.textColor(this));
		((TextView) findViewById(R.id.difficultyText)).setTextColor(Themes
				.textColor(this));

		// Set up the difficulty spinner
		Spinner difficultySpinner = (Spinner) findViewById(R.id.difficultySpinner);
		difficultySpinner
				.setOnItemSelectedListener(new OnItemSelectedListener() {
					public void onItemSelected(AdapterView<?> parent,
							View view, int pos, long id) {
						// Update the difficulty preference
						getSharedPreferences(GameMenu.GAME_PREFS, 0).edit()
								.putInt(GameMenu.DIFFICULTY, pos + 3).commit();
					}

					public void onNothingSelected(AdapterView<?> arg0) {
					}
				});
		difficultySpinner.setSelection(getSharedPreferences(
				GameMenu.GAME_PREFS, 0).getInt(GameMenu.DIFFICULTY, 3) - 3);

		// Set up the Themes spinner
		Spinner s = (Spinner) findViewById(R.id.spinner2);
		s.setSelection(getSharedPreferences(SETTINGS_PREFS, 0).getInt(THEME,
				Themes.DEFAULT_THEME));
		s.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> parent, View view,
					int pos, long id) {
				if (pos != getSharedPreferences(SETTINGS_PREFS, 0).getInt(
						THEME, 0)) {
					SharedPreferences.Editor editor = getSharedPreferences(
							SETTINGS_PREFS, 0).edit();
					editor.putInt(THEME, pos);
					editor.commit();
					Themes.changeToTheme(SettingsMenu.this, pos);
					if (pos == Themes.CUSTOM_POS)
						startActivity(new Intent(SettingsMenu.this,
								CustomThemeCreator.class));
				}
			}

			public void onNothingSelected(AdapterView<?> arg0) {
			}

		});
	}

	/**
	 * OnClickListener for Delete All High Scores button.
	 * 
	 * @param v
	 *            - The view which was clicked.
	 */
	public void deleteHighScores(View v) {
		// Create an AlertDialog to warn the user.
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		TextView tv = new TextView(this);
		tv.setText(DELETE_WARNING_TEXT);
		tv.setTextColor(Themes.textColor(this));
		tv.setTextSize(DELETE_WARNING_TEXT_SIZE);
		tv.setGravity(Gravity.CENTER);
		tv.setPadding(DELETE_WARNING_HORIZONTAL_PADDING,
				DELETE_WARNING_VERTICAL_PADDING,
				DELETE_WARNING_HORIZONTAL_PADDING,
				DELETE_WARNING_VERTICAL_PADDING);
		Themes.setBackgroundColor(this, tv);
		builder.setCustomTitle(tv)
				.setPositiveButton("Yes",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								HighScoreManager hsm = new HighScoreManager(
										getApplicationContext());
								hsm.removeAllHighScores();
							}
						})
				.setNegativeButton("No", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						dialog.cancel();
					}
				}).show();
	}

	/**
	 * Testing only method used to generate random high scores to populate the
	 * High Scores screen.
	 * 
	 * @param v
	 */
	public void addDummyHighScores(View v) {
		final ProgressDialog pDialog = ProgressDialog.show(this, "",
				"Generating High Scores", true, false);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
		new Thread(new Runnable() {

			public void run() {
				HighScoreManager hsm = new HighScoreManager(SettingsMenu.this);
				Bitmap image = BitmapFactory.decodeResource(getResources(),
						R.drawable.ic_launcher);
				for (Difficulty d : Difficulty.values()) {
					for (int i = 0; i < 20; i++) {
						hsm.update((int) (Math.random() * (double) 2000),
								(long) (Math.random() * (double) 1200000),
								image, d);
					}
				}
				pDialog.dismiss();
			}
		}).start();
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
	}
}
